package com.hsp.demo_07_money.money;

import javax.lang.model.element.VariableElement;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class MoneyChargeAPP{
    /*
    System.out.println("\n==============零钱通菜单=============");
            System.out.println("\t\t\t 1、零钱通明细");
            System.out.println("\t\t\t 2、收益入账");
            System.out.println("\t\t\t 3、消费");
            System.out.println("\t\t\t 4、退出");
     */
    //打印结果，通过字符串拼接得来
    String details = "---------------零钱通明细---------------" + "\n";
    //输入或者消费的名目
    String moneyName = "";
    //每次入账或消费的金额
    double money;
    //总余额
    double balance;
    //标识字段，表明是否退出菜单，true为不退出， false为退出
    boolean flag = true;

    Scanner scanner = new Scanner(System.in);
    //接受用户传递的参数
    String result;

    //日期的格式化对象
    SimpleDateFormat format = new SimpleDateFormat("YYYY-MM-dd HH:mm");


    public void showInfo(){
        do{
            System.out.println("===============零钱通菜单===============" + "\n");
            System.out.println("\t\t\t 1、零钱通明细");
            System.out.println("\t\t\t 2、收益入账");
            System.out.println("\t\t\t 3、消费");
            System.out.println("\t\t\t 4、退出");

            System.out.print("请输入选项(1-4)：");
            result = scanner.next();

            //判断用户属于的参数
            switch (result){
                case "1":
                    this.details();
                    break;
                case "2":
                    this.earning();
                    break;
                case "3":
                    this.consume();
                    break;
                case "4":
                    this.exit();
                    break;
                default:
                    System.out.println("输入的内容不正确，请重新输入！");
            }
        }while(flag);

        System.out.println("===============退出成功===============");
    }

    /**
     * 零钱通明细
     */
    private void details(){
        System.out.println(details);
    }

    /**
     * 收入
     */
    private void earning(){
        System.out.print("收益入账金额：");
        //接受用户输入的收益入账金额
        money = scanner.nextDouble();
        //校验输入金额是否正确 1、收益入账金额需 > 0
        if(money <= 0){
            System.out.println("收益入账金额需大于0");
            return;
        }
        balance += money;
        details += "收益入账" + "\t" + money + "\t" + format.format(new Date()) + "\t"
                    + balance + "\n";
    }

    /**
     * 消费
     */
    private void consume(){
        System.out.print("请输入消费名目：");
        moneyName = scanner.next();
        //scanner必须要有输入，才会继续往下走，所以这里不需要判断。
        /*if(moneyName.length()==0){
            System.out.println("消费名目不能为空");
            return;
        }*/
        System.out.print("请输入消费的金额：");
        //接受用户传递的消费的金额
        money = scanner.nextDouble();
        //进行金额的校验： 1、该金额 < 0  2、 消费的金额 <= balance
        if(money <= 0 || money > balance){
            System.out.println("消费的金额必须大于0.0, 小于等于" + balance);
            return;
        }
        balance -= money;
        details += moneyName + "\t-" + money + "\t" + format.format(new Date()) + "\t"
                    + balance + "\n";

    }

    /**
     * 退出
     */
    private void exit(){
        System.out.print("请确认是否需要退出(y/n)：");
        String exit = scanner.next();
       if(!"y".equals(exit)&&!"n".equals(exit)){
           System.out.println("输入不正确,需输入(y/n)！");
           return;
       }
       if("y".equals(exit)){
           flag = false;
       }
    }
}
